﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;

namespace Sharp2DTest
{
    static class ProjectNeonHelper
    {
        /// <summary>
        /// 学习并引用自MSDN的毛玻璃效果生成方法
        /// </summary>
        /// <param name="glassHost"></param>
        /// <param name="BlurAmount"></param>
        /// <param name="ColorSource"></param>
        public static void InitializeFrostedGlass(UIElement glassHost, float BlurAmount, Color ColorSource)
        {
            Visual hostVisual = ElementCompositionPreview.GetElementVisual(glassHost);
            Compositor compositor = hostVisual.Compositor;
            // 创建玻璃效果，需要Win2D的Nuget库,当然了，现在这个sharp2d也能做了
            var glassEffect = new Yinyue200.Sharp2D.Direct2D.Effects.GaussianBlurEffect()
            {
                BlurAmount = BlurAmount,
                BorderMode = Yinyue200.Sharp2D.Direct2D.Effects.EffectBorderMode.Hard,
                Source = new Yinyue200.Sharp2D.Direct2D.Effects.ArithmeticCompositeEffect
                {
                    MultiplyAmount = 0,
                    Source1Amount = 0.5f,
                    Source2Amount = 0.5f,
                    Source = new CompositionEffectSourceParameter("backdropBrush"),
                    Source2 = new Yinyue200.Sharp2D.Direct2D.Effects.ColorSourceEffect
                    {
                        Color = ColorSource
                    }
                }
                //Source = new Microsoft.Graphics.Canvas.Effects.ArithmeticCompositeEffect
                //{
                //    MultiplyAmount = 0,
                //    Source1Amount = 0.5f,
                //    Source2Amount = 0.5f,
                //    Source1 = new CompositionEffectSourceParameter("backdropBrush"),
                //    Source2 = new Microsoft.Graphics.Canvas.Effects.ColorSourceEffect
                //    {
                //        Color = ColorSource
                //    }
                //}
            };
            //var glassEffect2 = new Microsoft.Graphics.Canvas.Effects.GaussianBlurEffect()
            //{
            //    BlurAmount = BlurAmount,
            //    BorderMode = Microsoft.Graphics.Canvas.Effects.EffectBorderMode.Hard,
            //    Source = new Microsoft.Graphics.Canvas.Effects.ArithmeticCompositeEffect
            //    {
            //        MultiplyAmount = 0,
            //        Source1Amount = 0.5f,
            //        Source2Amount = 0.5f,
            //        Source1 = new CompositionEffectSourceParameter("backdropBrush"),
            //        Source2 = new Microsoft.Graphics.Canvas.Effects.ColorSourceEffect
            //        {
            //            Color = ColorSource
            //        }
            //    }
            //};
            //var Source2 = new Microsoft.Graphics.Canvas.Effects.ColorSourceEffect
            //{
            //    Color = Windows.UI.Color.FromArgb(1, 2, 3, 4)
            //};
            //var guid = SharpDX.Utilities.GetGuidFromType(typeof(Yinyue200.Sharp2D.Interop.IGraphicsEffectD2D1Interop));
            //System.Runtime.InteropServices.Marshal.QueryInterface(System.Runtime.InteropServices.Marshal.GetIUnknownForObject(Source2), ref guid, out IntPtr ppv);
            //var d2di = (Yinyue200.Sharp2D.Interop.IGraphicsEffectD2D1Interop)System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(ppv);
            //d2di.GetProperty(0,out IntPtr value);
            //var obj=System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(value);
            //  创建效果实例并设定源
            var backdropBrush = compositor.CreateBackdropBrush();
            var effectFactory = compositor.CreateEffectFactory(glassEffect);
            glassEffect.Dispose();
            var effectBrush = effectFactory.CreateBrush();

            effectBrush.SetSourceParameter("backdropBrush", backdropBrush);

            // 创建一个Visual对象来承载效果
            var glassVisual = compositor.CreateSpriteVisual();
            glassVisual.Brush = effectBrush;

            // 将模糊效果添加为可视化树的成员
            ElementCompositionPreview.SetElementChildVisual(glassHost, glassVisual);

            // 保证效果拥有者和可视化始终保持相同大小
            var bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size");
            bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual);

            glassVisual.StartAnimation("Size", bindSizeAnimation);
        }
    }
}
